{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "b17eafb8d1da4fcd8778d6946cfbb1bb",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "Plot(antialias=3, axes=['x', 'y', 'z'], axes_helper=1.0, background_color=16777215, camera=[5.247435829652697,…"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "\"\"\"Extract submesh boundaries.\"\"\"\n",
    "#Credit: https://fenicsproject.discourse.group/t/output-parts-of-boundary/537\n",
    "from fenics import *\n",
    "from mshr import *\n",
    "from numpy import array\n",
    "from numpy.linalg import norm\n",
    "\n",
    "domain = Box(Point(0,0,0), Point(10,10,10)) - Sphere(Point(5,5,5), 3)\n",
    "mesh = generate_mesh(domain, 32)\n",
    "exterior = BoundaryMesh(mesh, \"exterior\")\n",
    "\n",
    "def inSphere(x):\n",
    "    v = x - array([5, 5, 5])\n",
    "    return norm(v) < 3 + 1e2 * DOLFIN_EPS\n",
    "\n",
    "class SphereDomain(SubDomain):\n",
    "    def inside(self, x, on_boundary):\n",
    "        return inSphere(x)\n",
    "\n",
    "class BoxDomain(SubDomain):\n",
    "    def inside(self, x, on_boundary):\n",
    "        return not inSphere(x)\n",
    "\n",
    "sph = SubMesh(exterior, SphereDomain())\n",
    "box = SubMesh(exterior, BoxDomain())\n",
    "\n",
    "##################################\n",
    "from vtkplotter.dolfin import *\n",
    "\n",
    "embedWindow('k3d') # backends are: itkwidgets, k3d or False\n",
    "\n",
    "plot(sph, c='r')\n",
    "plot(box, c='y', wireframe=True, add=True, zoom=.7)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.8"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
